package modelo.hibernate.dao;

import java.util.ArrayList;
import java.util.List;

import modelo.hibernate.config.Sesion;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import modelo.entidad.Caracteristica;
import modelo.entidad.Pago;
import modelo.entidad.Vehiculo;

public class DaoCaracteristica {

	private Sesion sesionPostgres;
	
	public void agregarCaracteristica(Caracteristica dato) throws Exception{
		@SuppressWarnings("static-access")
		Session em = sesionPostgres.openSession();  
         Transaction tx = null;  
         try {    
        	 tx = em.beginTransaction();
              em.save( dato);   
              tx.commit();  
         } catch (Exception e) {  
             tx.rollback();            
             e.printStackTrace();
             throw e;
         } finally {  
             em.close();  
         } 
	}
	
	public Caracteristica obtenerCaracteristica(int id) throws Exception{		 
	    @SuppressWarnings("static-access")
		Session sesion = sesionPostgres.openSession();  
            Caracteristica dato = null;        
            try{
                dato = (Caracteristica) sesion.get(Caracteristica.class,  id);
            } catch (Exception e) {  
            e.printStackTrace();
           
            throw new Exception(e.getMessage(),e.getCause());
            }  finally {  
                sesion.close();  
            }  
           
	    return dato;
	}
	
	public List<Caracteristica> obtenerTodos() throws Exception {            
	      
		   List<Caracteristica> datos = new ArrayList<Caracteristica>();  
		   Session em = sesionPostgres.openSession();  	
	        try {  	
		    datos =  (List<Caracteristica>) em.createCriteria(Caracteristica.class).list();             
	        } catch (Exception e) {             
	       
	         throw new Exception(e.getMessage(),e.getCause());
	        } finally {  
	          em.close();  
	        } 
	       
	        return datos; 
		}
	
	public List<Caracteristica> getCaracteristicasporVehiculo(int idVehiculo) {
		
		List<Caracteristica> resultado = new ArrayList<Caracteristica>();
		List<Object[]> query = new ArrayList<Object[]>();
		Session em =sesionPostgres.openSession();
		
		try{			
			 String consulta = "Select a.nombre from caracteristica a, caracteristicadelvehiculo b where a.id=b.id_caracteristica and b.id_vehiculo=:dato";
			 query = em.createQuery(consulta).setParameter("dato", idVehiculo).list();			 
		
			 for (Object[] obj : query){
				Caracteristica a  = new Caracteristica();
				a.setNombre((String) obj[0]);
				resultado.add(a);
				}
			 }catch (Exception e) {
				e.printStackTrace();
			}
			return resultado;
	}
	
	public void actualizarCaracteristica(Caracteristica dato) throws Exception{
		@SuppressWarnings("static-access")
		Session em = sesionPostgres.openSession();  
         Transaction tx = null;  
         try {    
        	 tx = em.beginTransaction();
              em.update(dato);   
              tx.commit();  
         } catch (Exception e) {  
             tx.rollback();            
             e.printStackTrace();
             throw e;
         } finally {  
             em.close();  
         } 
	}
	
	
}
